package com.ruoyi.syf.task;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.CalendarUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.syf.common.constant.HolidayType;
import com.ruoyi.syf.domain.HolidayInfo;
import com.ruoyi.syf.service.HolidayInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.DateFormatSymbols;
import java.util.*;

/**
 * 节假日
 */
@Slf4j
@Component
public class HolidayTask {

    private static final String URL = "https://timor.tech/api/holiday/year/";

    @Autowired
    private HolidayInfoService holidayInfoService;

    /**
     * 每年1月1日0点23分执行
     * 生成年度日期
     */
    @Scheduled(cron = "0 23 0 1 1 ?")
    public void generateYearData() {
        int year = Integer.parseInt(DateUtils.dateTimeNow("yyyy"));
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(GregorianCalendar.DAY_OF_YEAR, 1);
        calendar.set(GregorianCalendar.YEAR, year);
        // 循环输出一年的日期
        List<HolidayInfo> list = new ArrayList<>();
        while (calendar.get(GregorianCalendar.YEAR) == year) {
            Date date = calendar.getTime();
            HolidayInfo holidayInfo = new HolidayInfo();
            holidayInfo.setYear(calendar.get(GregorianCalendar.YEAR));
            holidayInfo.setMonth(calendar.get(GregorianCalendar.MONTH) + 1);
            holidayInfo.setDay(calendar.get(GregorianCalendar.DATE));
            //周名称
            holidayInfo.setWeekName(new DateFormatSymbols().getWeekdays()[calendar.get(GregorianCalendar.DAY_OF_WEEK)]);
            int weekDay = calendar.get(GregorianCalendar.DAY_OF_WEEK);
            int boo = (weekDay == 1 || weekDay == 7) ? HolidayType.WEEKEND.getCode() : HolidayType.WEEK.getCode();
            //判断日期类型 0工作日 1周末 2节假日 3调休
            holidayInfo.setType(boo);
            holidayInfo.setKeyDate(DateFormatUtils.format(date, "MM-dd"));
            holidayInfo.setCurDate(date);
            Calendar instance = Calendar.getInstance();
            instance.setTime(date);
            CalendarUtil calendarUtil = new CalendarUtil(instance);
            //农历日 初一~三十
            holidayInfo.setLunars(calendarUtil.getChinaDayString(0));
            //农历日 例如: 二零三年九月初一
            holidayInfo.setLunarsDate(calendarUtil.getDay());
            list.add(holidayInfo);
            calendar.add(GregorianCalendar.DAY_OF_YEAR, 1);
        }
        holidayInfoService.saveBatch(list);
        syncHoliday(year);
    }

//    @PostConstruct
//    public void run(){
//        syncHoliday(2024);
//    }

    /**
     * 同步节假日
     */
    public void syncHoliday(int year) {
        String result = HttpUtils.sendGet(URL + year);
        JSONObject parse = JSONObject.parse(result);
        JSONObject holidayJson = (JSONObject) parse.get("holiday");
        Map<String, Object> map = holidayJson.toJavaObject(Map.class);
        List<HolidayInfo> holidayList = holidayInfoService.list(new LambdaQueryWrapper<HolidayInfo>()
                .eq(HolidayInfo::getYear, year));
        for (HolidayInfo holiday : holidayList) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                if (holiday.getKeyDate().equals(key)) {
                    JSONObject valueJson = (JSONObject) JSONObject.parse(String.valueOf(entry.getValue()));
                    Boolean holidayFlag = (Boolean) valueJson.get("holiday");
                    String name = (String) valueJson.get("name");
                    holiday.setType(holidayFlag ? HolidayType.HOLIDAY.getCode() : HolidayType.ADJUST.getCode());
                    holiday.setName(name);
                    holidayInfoService.updateById(holiday);
                }
            }
        }
    }

}
